import { ComponentInstance } from "types"

export const enum LifeCycle {
  BEFORE_MOUNT = 'bm',
  MOUNTED = 'm',
  BEFORE_UPDATE = 'bu',
  UPDATED = 'u',
  BEFORE_UNMOUNT = 'bum',
  UNMOUNTED = 'um',
  RENDER_TRACKED = 'rtc',
  RENDER_TRIGGERED = 'rtg'
}

function injectHook(
  phase:LifeCycle, hook:Function, target:ComponentInstance|null) {
  if(!target) return 
  // @ts-ignore
  target.life[phase].push(hook)
}

function createLifeCycleHook(phase:LifeCycle) {
  return function(hook:Function, instance=window.currentInstance) {
    injectHook(phase, hook, instance)
  }
}

export function invokeLifeCycleHook(arr:Function[]) {
  arr.forEach(hook=>hook())
}

export const onBeforeMount = createLifeCycleHook(LifeCycle.BEFORE_MOUNT)
export const onMounted = createLifeCycleHook(LifeCycle.MOUNTED)
export const onBeforeUpdate = createLifeCycleHook(LifeCycle.BEFORE_UPDATE)
export const onUpdated = createLifeCycleHook(LifeCycle.UPDATED)
export const onRenderTracked = createLifeCycleHook(LifeCycle.RENDER_TRACKED)
export const onRenderTriggered = createLifeCycleHook(LifeCycle.RENDER_TRIGGERED)

export function onErrorCaptured() {}

